<template>
	<view>dfdsgdg</view>
</template>
<script>
import {
	userLogin,
	resolution,
} from "@/utils/utils.js";
import eventTracking from "@/utils/eventTracking.js";
import {
	mapGetters,
	mapMutations,
	mapActions
} from "vuex";
import {
	getCreditList
} from '@/api/my.js'
// import {
// 	unReadCount
// } from '@/api/custom.js'
import tabbarNoticeMixin from '@/mixins/tabbar-notice'; // 引入tabbarNoticeMixin
import WebSocket from "@/utils/websocket4.js"
export default {
	mixins: [tabbarNoticeMixin], // 使用tabbarNoticeMixin
	data() {
		return {
			_isLaunching: true,
		};
	},
	computed: {
		// 使用对象展开运算符将 getter 混入 computed 对象中
		...mapGetters(["token"]),
	},
	onLaunch: async function (e) {
		// console.log("App Launch",uni.getSystemInfoSync());
		const systemInfo = resolution()
		console.log("系统模型:", systemInfo.model)
		const systemData = eventTracking.event('AI_CUSTOMIZATION')
		console.log("systemData", systemData);

		// if (this._isLaunching) return
		// this._isLaunching = true
		try {
			if (!uni.getStorageSync("token") || !this.token) {
				await new Promise((resolve) => {
					userLogin(() => {
						this.init()
						resolve()
					}, { shareId: "" })
				})
			} else {
				this.init()
			}
		} finally {
			this._isLaunching = false
		}
	},

	onShow: function () {
		console.log("App Show");
		let updateManager = uni.getUpdateManager();
		if (!updateManager) {
			return;
		} else {
			//新版本更新
			if (uni.canIUse("getUpdateManager")) {
				//判断当前微信版本是否支持版本更新

				updateManager.onCheckForUpdate(function (res) {
					if (res.hasUpdate) {
						// 请求完新版本信息的回调
						updateManager.onUpdateReady(function () {
							uni.showModal({
								title: "更新提示",
								content: "新版本已经准备好，是否重启应用？",
								success: function (res) {
									if (res.confirm) {
										// 新的版本已经下载好，调用 applyUpdate 应用新版本并重启
										updateManager.applyUpdate();
									}
								},
							});
						});
						updateManager.onUpdateFailed(function () {
							uni.showModal({
								// 新的版本下载失败
								title: "已经有新版本了哟~",
								content: "新版本已经上线啦~，请您删除当前小程序，到微信 “发现-小程序” 页，重新搜索打开哦~",
							});
						});
					} else { }
				});
			} else {
				uni.showModal({
					// 如果希望用户在最新版本的客户端上体验您的小程序，可以这样子提示
					title: "溫馨提示",
					content: "当前微信版本过低，部分功能无法使用，请升级到最新微信版本后重试。",
				});
			}
		}
	},
	onHide: function () {
		console.log("App Hide");
	},
	methods: {
		...mapMutations(['steShowPrivacy', 'setMessages', 'setTotalCredits']),
		...mapActions(['getAreaOptions', 'setPhoneModelList']),
		init() {
			// this.$nextTick(() => {
			// 	 // 刷新未读消息
			// 	setTimeout(() => this.getUnReadCount(), 500)
			// })
			this.getPrivacySetting()
			this.setPhoneModelList()
			this.getAreaOptions()
			getCreditList({
				pageSize: 1
			}).then(res => {
				this.setTotalCredits(res.data.totalCredits)
			})
			if (!WebSocket.client) {
				this.initWS()
			}
		},
		// async getUnReadCount() {
		// 	const res = await unReadCount()
		// 	console.log("getUnReadCount", res)
		// 	if(res.code === 200){
		// 		//unReadCount大于0就显示红色数字，如果==0则判断hasUnRead是否为true,为true则显示红点
		// 		if(res.data.unReadCount > 0){
		// 			uni.setTabBarBadge({
		// 				index: 2,
		// 				text: res.data.unReadCount.toString()
		// 			})
		// 		}else{
		// 			if(res.data.hasUnRead){
		// 				uni.showTabBarRedDot({
		// 					index: 2
		// 				})
		// 			}else{
		// 				uni.hideTabBarRedDot({
		// 					index: 2
		// 				})
		// 			}
		// 		}
		// 	}
		// },

		getPrivacySetting() {
			if (wx.getPrivacySetting) {
				wx.getPrivacySetting({
					success: (res) => {
						console.log("getPrivacySettinggetPrivacySettinggetPrivacySetting");
						console.log(
							res
						); // 返回结果为: res = { needAuthorization: true/false, privacyContractName: '《xxx隐私保护指引》' }
						if (res.needAuthorization) {
							// 需要弹出隐私协议
							this.steShowPrivacy(true);
						} else {
							// 用户已经同意过隐私协议，所以不需要再弹出隐私协议，也能调用已声明过的隐私接口
							this.steShowPrivacy(false)
						}
					},
					fail: () => { },
					complete: () => { },
				});
			}
		},
		initWS() {
			WebSocket.subscribe(`/generateCustomImageSuccess`, (res) => {
				console.log('收到消息2：', res)
				this.checkUnReadMessage();
				let data = JSON.parse(res.body)
				console.log(data);
				this.setMessages(data)
				// 如果是我的定制页，则不出现消息提醒
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				console.log('currentPage', currentPage.route == 'pages/myCustom')

				if ((data.customImage.status == 0 && data.customImage.status == 0)) {
					uni.showTabBarRedDot({
						index: 2
					})
					uni.showToast({
						title: '上传成功',
					})
					setTimeout(() => {
						uni.switchTab({
							url: '/pages/myCustom'
						})
					}, 2000);
					return
				}

				if ((data.customImage.status == 3 || data.customPhoneCase.status == 3) && currentPage.route != 'pages/myCustom') {
					uni.showTabBarRedDot({
						index: 2
					})
					uni.showModal({
						content: '您有一张图片已定制完成，可到我的定制页查看',
						confirmText: '立即查看',
						cancelText: '稍后查看',
						success: function (res) {
							if (res.confirm) {
								uni.switchTab({
									url: '/pages/myCustom'
								})
							}
						}
					});
				}
			})

			WebSocket.subscribe(`/paySuccess`, (res) => {
				console.log('支付消息===>：', res)
				let data = JSON.parse(res.body)
				console.log(data);
				// 如果是我的定制页，则不出现消息提醒
				const pages = getCurrentPages();
				const currentPage = pages[pages.length - 1];
				uni.showToast({
					title: '支付成功',
					icon: 'success',
					duration: 2000,
				})
				if (currentPage.route == 'pages/myCustom/customView') {
					uni.$emit('payCustomViewStatus', data)
				} else if (currentPage.route == 'pages/product/productDetail') {
					uni.$emit('payProductStatus', data)
				}
			})
		},
	},
};
</script>

<style lang="scss">
@import "@/uni_modules/uview-ui/index.scss";
//公共样式
@import "@/static/style/comm.scss";

.slider-range {
	padding-top: 0px !important;
}

page {
	background-color: #f6f6f6;
	width: 100%;
	height: 100%;
	font-size: 24rpx;
	box-sizing: border-box;
	color: $uni-text-color;
}

view {
	box-sizing: border-box;
}
</style>